const listMixin = {
  data() {
    return {
      tableData: [],
      page: {
        index: 1,
        size: 10,
        total: 0
      },
      multipleSelected: [],
      delLoading: false,
      timer:null,
      onlyDelOne:false
    }
  },
  computed: {
    isBatchesDel() {
      return this.multipleSelected.length > 1 ? '批量删除' : '删除'
    },
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleRowClick(row, column, event) {
      // this.$refs['table'].toggleRowSelection(row)
    },
    refresh() {
      location.reload()
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.index = val
      this.fetchData()
    },
    toAddPage() {
      this.$router.push(this.mixinParams.addUrl)
    },
    edit() {
      if (this.multipleSelected.length === 0) {
        this.$message.info('请选择要编辑的记录')
      }else if (this.multipleSelected.length > 1){
        this.$message.info('您一次只能编辑一条记录')
      } else {
        if (!this.mixinParams.editUrl.endsWith('/')) {
          this.mixinParams.editUrl += '/'
        }
        this.$router.push(this.mixinParams.editUrl + this.multipleSelected[0])
      }
    },
    handleSelectionChange(arr) {
      this.multipleSelected = []
      for (const val of arr) {
        this.multipleSelected.push(val[this.mixinParams.propertyName])
      }
    },
    del() {
      if (this.multipleSelected.length === 0) {
        this.$message.warning('请选择要删除的记录')
        return
      }
      if (this.delLoading) {
        return
      }
      this.$confirm('确定删除吗', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delLoading = true
        let promise = null
        if (!this.mixinParams.deleteMethod) {
          promise = this.mixinParams.API.delete(this.multipleSelected)
        } else {
          promise = this.mixinParams.API[this.mixinParams.deleteMethod](this.multipleSelected)
        }
        promise.then(res => {
          this.delLoading = false
/*          if (this.multipleSelected.length === this.tableData.length) {
            this.page.index = --this.page.index === 0 ? 1 : this.page.index
          }*/
          this.fetchData()
        }).catch(err => {
          this.delLoading = false
          console.log(err)
        })
      }).catch(() => {
      })
    },
    delOne(){
      if(this.multipleSelected.length > 1){
        this.$message.warning('您一次只能删除一条记录')
        return
      }
      this.del()
    },
    getObjFromTableData(propertyName) {
      for (let val of this.tableData) {
        if (val[propertyName] === this.multipleSelected[0]) {
          return JSON.parse(JSON.stringify(val))
        }
      }
    },
    // 搜索防抖
    AntiShake(){
      clearTimeout(this.timer)
      this.timer = setTimeout(()=>{
        this.fetchData()
      },400)
    },
    openEditDialog(isEdit) {
      if(isEdit){
        if(this.multipleSelected.length === 0){
          this.$message.info('请选择要编辑的记录')
          return
        }
        if(this.multipleSelected.length > 1){
          this.$message.info('您一次只能编辑一条记录')
          return
        }
        this.$refs['save-page'].open(this.multipleSelected[0])
      }else{
        this.$refs['save-page'].open()
      }
    },
    switchType(str) {
      let type = ''
      switch (str) {
        case '审批中':
          type = 'default'
          break
        case '已审批':
          type = 'success'
          break
        case '退回':
          type = 'danger'
          break
      }
      return type
    },
  }
}

export default listMixin
